/*
 * @Author: wangqizhang
 * @Date: 2024-04-21 19:24:53
 * @LastEditors: wangqizhang
 * @LastEditTime: 2024-05-24 15:28:42
 * @FilePath: /suixinji-react/src/router/index.ts
 * @Description:
 *
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
import { createBrowserRouter, createHashRouter } from 'react-router-dom';
import React, { lazy } from 'react';
import WorkPlace from '../page/workplace/index';
import NotFound from '../components/404';
import NoPower from '../components/401';
import Home from '../page/home/index';

import { hasSessionStorage } from '../untils';

export const routers = [
  {
    path: '/',
    Component: lazy(() => import('../page/guidPage')),
  },
  {
    path: '/login',
    Component: lazy(() => import('../page/login/index')),
  },
  {
    path: '/home',
    Component: () =>
      hasSessionStorage('token')
        ? React.createElement(Home)
        : React.createElement(NoPower),
    children: [
      {
        Component: WorkPlace,
        index: true,
        name: '工作台',
      },
      {
        path: 'note/:id',
        Component: lazy(() => import('../page/note/index')),
        name: '笔记',
      },
      {
        path: 'group_config',
        Component: lazy(() => import('../page/group/index')),
        name: '分组管理',
      },
      {
        path: 'test',
        Component: lazy(() => import('../page/test')),
        name: '测试',
      },
    ],
  },
  {
    path: '*',
    Component: NotFound,
  },
];
const router =
  import.meta.env.MODE === 'development'
    ? createBrowserRouter(routers)
    : createHashRouter(routers);
export const getRouterItem = (path: string) => {
  let result = false;
  function _getItems(data: any[], path: string) {
    data.forEach((item) => {
      if (item.path) {
        if (item.path === path) {
          result = item;
        } else if (item.children && item.children.length > 0) {
          _getItems(item.children, path);
        }
      } else {
        if (item.children && item.children.length > 0) {
          _getItems(item.children, path);
        }
      }
    });
  }
  _getItems(routers, path);
  return result;
};

export default router;
